import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户信息数组
    userInfo: [],
    // 默认用户名
    nameValue: '',
    // 默认用户密码
    psValue: '',
    // 登录状态，1代表登录成功，0代表登录失败（未登录），默认是未登录
    loginStatus: 0,
    // 选中图书id
    chooseBookId: '',
    // 购物车中的数组
    carList: [],
    // 购买数量
    buyNumber: 1
  },
  mutations: {
    setInputName (state, val) {
      state.nameValue = val
    },
    setInputPs (state, val) {
      state.psValue = val
    },
    setChooseBookId (state, val) {
      state.chooseBookId = val
    },
    setCarList (state, list) {
      state.carList.unshift(list)
    },
    setBuyNumber (state, val) {
      state.buyNumber = val
    },
    initUserInfo (state, user) {
      state.userInfo = user
    },
    // 判定用户输入的昵称和密码是否和数据库中的数据相匹配
    showLogin (state) {
      let newUserInfo = state.userInfo.filter(x => x.user_name === state.nameValue)
      if (newUserInfo) {
        if (newUserInfo[0].user_password === state.psValue) {
          state.loginStatus = 1
        } else {
          state.loginStatus = 0
        }
      } else {
        state.loginStatus = 0
      }
      return state.loginStatus
    },
    clear (state) {
      state.nameValue = ''
      state.psValue = ''
    }
  },
  actions: {
  },
  getters: {
  },
  modules: {
  }
})
